package org.itec.yeast.trdo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.itec.yeast.db.DataManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TRStrainSQL {
	private static final Logger log = LoggerFactory.getLogger(TRStrainSQL.class);
	private static final String SQL_INSERT = "insert into strain("
											+ "ALLELE_idALLELE, GENE_idGENE, Strain_Name, Marker_Type"
											+ ") values (?, ?, ?, ?)";
	
	private Object[] getParams(TRStrain strain){
		Object[] params = new Object[]{
			strain.getAlleleID(),
			strain.getGeneID(),
			strain.getStrainName(),
			strain.getStrainType()
		};
		
		return params;
	}
	
	public int getStrainID(String strainName){
		String sql = "select idSTRAIN from strain where Strain_Name = ?";
		Object[] params = new Object[]{strainName};
		List strainID = DataManager.executeQuery(sql, params);
		if (strainID == null || strainID.size() == 0){
			return -1;
		}
		
		Map<String, Integer> result = (Map) strainID.get(0);
		return result.get("idSTRAIN");
	}
	
	public Map<String, Integer> getStrainIDs(){
		Map<String, Integer> strainIDs = new HashMap<String, Integer>();
		String sql = "select idSTRAIN, Strain_Name from strain";
		List list = DataManager.executeQuery(sql, null);
		if (list == null || list.size() == 0){
			log.error("表STRAIN中没有数据。请先获取STRAIN信息");
		} else {
			for (int i = 0; i < list.size(); i++){
				Map map = (Map) list.get(i);
				strainIDs.put((String)map.get("Strain_Name"), (Integer)map.get("idSTRAIN"));
			}
		}
		return strainIDs;
	}
	
	public Map<String, Integer> getStrainIDs(int type){
		Map<String, Integer> strainIDs = new HashMap<String, Integer>();
		String sql = "select idSTRAIN, Strain_Name from strain where Marker_Type = ?";
		Object[] params = new Object[]{type};
		List list = DataManager.executeQuery(sql, params);
		if (list == null || list.size() == 0){
			log.error("表STRAIN中没有数据。请先获取STRAIN信息");
		} else {
			for (int i = 0; i < list.size(); i++){
				Map map = (Map) list.get(i);
				strainIDs.put((String)map.get("Strain_Name"), (Integer)map.get("idSTRAIN"));
			}
		}
		return strainIDs;
	}
	
	public void insert(TRStrain strain){
		Object[] params = getParams(strain);
		int result = DataManager.executeUpdate(SQL_INSERT, params);
		if (result != 0){
			log.info("插入Strain数据成功");
		} else {
			log.info("未插入Strain数据");
		}
	}
	
	public void insert(List<TRStrain> strains){
		int result = -1;
		List<Object[]> list = new ArrayList<Object[]>();
		long start = System.currentTimeMillis();
		for (int i = 0; i < strains.size(); i++){
			TRStrain strain = strains.get(i);
			Object[] params = getParams(strain);
			list.add(params);
		}
		
		result = DataManager.executeBatchInsert(SQL_INSERT, list);
		long end = System.currentTimeMillis();
		log.info("插入" + result + "条Strain记录，共用" + (end - start) + "ms");
	}
	
	public boolean isStrainExist(TRStrain strain){
		boolean exist = false;
		String sql = "select idSTRAIN from strain where Strain_Name = ? and Marker_Type = ?";
		Object[] params = new Object[]{strain.getStrainName(), strain.getStrainType()};
		List list = DataManager.executeQuery(sql, params);
		if (list != null && list.size() > 0){
			exist = true;
		}
		
		return exist;
	}
	
	public List<String> getStoredStrains(int markerType){
		List<String> result = new ArrayList<String>();
		List<Map> tmp = new ArrayList<Map>();
		String sql = "select Strain_Name from strain where Marker_Type = ?";
		Object[] params = new Object[]{markerType};
		tmp = DataManager.executeQuery(sql, params);
		if (tmp != null){
			for (int i = 0; i < tmp.size(); i++){
				Map<String, String> name = tmp.get(i);
				result.add(name.get("Strain_Name"));
			}
		}
		
		return result;
	}
	
	public static void main(String[] args){
		TRStrainSQL sql = new TRStrainSQL();
		Map<String, Integer> maps = sql.getStrainIDs(0);
		
	}
}
